/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ShortProgressPanel.java
*
* Created on November 13, 2007, 4:49 PM
 */
package javautil.swing.components;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.SwingUtilities;

/**
 *
 * @author  stewari1
 */
public class ShortProgressPanel extends javax.swing.JPanel implements PropertyChangeListener {


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JProgressBar progressBar;

    /** Field description */
    private javax.swing.JLabel status_JLabel;


    // End of variables declaration//GEN-END:variables

    /** Creates new form ShortProgressPanel */
    public ShortProgressPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        status_JLabel = new javax.swing.JLabel();
        progressBar   = new javax.swing.JProgressBar();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 5, 0, 0));
        status_JLabel.setText("Progress Status");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addComponent(
                    status_JLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 198,
                    Short.MAX_VALUE).addGap(29, 29, 29).addComponent(
                        progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 222,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                status_JLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 19,
                Short.MAX_VALUE).addComponent(
                    progressBar, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));

    }    // </editor-fold>//GEN-END:initComponents

    @Override
    public void propertyChange(PropertyChangeEvent evt) {

        if (ProgressSupport.PROP_PROGRESS.equals(evt.getPropertyName())) {
            setProgress(((Integer) evt.getNewValue()).intValue());
        }

        if (ProgressSupport.PROP_STATUS_INFO.equals(evt.getPropertyName())) {
            setStatusInfo((String) evt.getNewValue());
        }

        if (ProgressSupport.PROP_SHOW_PROGRESS.equals(evt.getPropertyName())) {

            System.out.println("inside show progress ;;;");
            setShowProgress(((Boolean) evt.getNewValue()).booleanValue());
        }
    }

    public void setShowProgress(final boolean val) {

        Runnable runnable = new Runnable() {

            public void run() {
                progressBar.setVisible(val);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    public void setStatusInfo(final String str) {

        Runnable runnable = new Runnable() {

            public void run() {
                status_JLabel.setText(str);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    public void setProgress(final int progress) {

        Runnable runnable = new Runnable() {

            public void run() {
                progressBar.setValue(progress);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }
}
